package com.landeb.shop.huo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.landeb.shop.R;
import com.landeb.shop.huo.bean.BeanGoodDetailActBsd;
import com.landeb.shop.huo.view.FixedGridView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by blade on 2016/12/7.
 */

public class ActGoodDetailBsdLVAdapter extends BaseAdapter {

    private List<BeanGoodDetailActBsd.DataBean.AttrsBean> mList;
    private List<BeanGoodDetailActBsd.DataBean.StocksBean> mStockList;
    private Context mContext;
    private LayoutInflater mInflater;

    private Map<Integer, String> mIdMap;
    private Map<Integer, String> mDesMap;
    private TextView tvPrice, tvStock, tvDes;

    private OnSelectFinishedListener mListner;

    public ActGoodDetailBsdLVAdapter(Context context, List<BeanGoodDetailActBsd.DataBean.AttrsBean> list) {
        mContext = context;
        mList = list;

        mInflater = LayoutInflater.from(context);
    }

    public ActGoodDetailBsdLVAdapter(Context context, List<BeanGoodDetailActBsd.DataBean.AttrsBean> list,
                                     List<BeanGoodDetailActBsd.DataBean.StocksBean> StockList,
                                     TextView tvPrice, TextView tvStock, TextView tvDes) {
        mContext = context;
        mList = list;
        mStockList = StockList;
        mIdMap = new HashMap<>();
        mDesMap = new HashMap<>();
        mInflater = LayoutInflater.from(context);
        this.tvPrice = tvPrice;
        this.tvStock = tvStock;
        this.tvDes = tvDes;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        ViewHolder holder = null;
        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.item_bottom_sheet_dialog_sku_lv, parent, false);

            holder = new ViewHolder();
            holder.mTitle = (TextView) convertView.findViewById(R.id.tv_itemName_item_bottom_sheet_dialog_sku_lv);
            holder.mGridView = (FixedGridView) convertView.findViewById(R.id.fixedGv_item_bottom_sheet_dialog_sku_lv);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mTitle.setText(mList.get(position).getName());

        final ActGoodDetailBsdLvGvAdapter mAdapter = new ActGoodDetailBsdLvGvAdapter(mContext, mList.get(position).getChild());
        holder.mGridView.setAdapter(mAdapter);

        holder.mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int gvPosition, long id) {

                for (BeanGoodDetailActBsd.DataBean.AttrsBean.ChildBean s : mList.get(position).getChild()) {

                    s.setIs_select("0");

                }
                mList.get(position).getChild().get(gvPosition).setIs_select("1");



//                StringBuilder sb = new StringBuilder();
//                for (BeanGoodDetailActBsd.DataBean.AttrsBean.ChildBean s : mList.get(position).getChild()) {
//
//                    if (s.getIs_select().equals("1")) {
//                        sb.append(s.getName());
//                    }
//
//                }
//
//                Log.e("onItemClick: ", "p+" + position + "name+" + sb.toString());



                mAdapter.notifyDataSetChanged();

                mIdMap.put(position, mList.get(position).getChild().get(gvPosition).getId());
                mDesMap.put(position, mList.get(position).getChild().get(gvPosition).getName());

                tvDes.setText(getDes());
                getStock();

            }
        });

        final ViewHolder finalHolder = holder;
        holder.mGridView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_MOVE:
                        finalHolder.mGridView.getParent().getParent().requestDisallowInterceptTouchEvent(true);
                        break;

                    case MotionEvent.ACTION_UP:


                    case MotionEvent.ACTION_CANCEL:
                        finalHolder.mGridView.getParent().getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                }

                return false;
            }
        });

        return convertView;
    }

    private class ViewHolder {
        private TextView mTitle; //文字
        private FixedGridView mGridView;

    }

    public interface OnSelectFinishedListener {

        public void onFinished(String attrs);

    }

    public void setOnSelectFinshedListener(OnSelectFinishedListener listener) {

        mListner = listener;

    }

    private void getStock() {

        if (mList.size() != 0 && mIdMap.keySet().size() == mList.size()) {

            ArrayList<Integer> keyList = new ArrayList<>(mIdMap.keySet());
            Collections.sort(keyList);

            StringBuilder sb = new StringBuilder();

            for (int i : keyList) {
                sb.append(mIdMap.get(i) + ",");
            }

            String result = sb.toString();


            if (result.endsWith(",")) {

                result = result.substring(0, result.length() - 1);

            }

            Log.e("getStock:++++++ ", result);

            for (BeanGoodDetailActBsd.DataBean.StocksBean b : mStockList) {

                if (b.getAttr().equals(result)) {
                    tvPrice.setText("¥" + b.getPrice());
                    tvStock.setText("库存：" + b.getStock() + "件");

                    if (mListner != null) {
                        mListner.onFinished(result);
                    }
                }

            }

        }


    }

//    设置已选项目的方法

    private String getDes() {

        StringBuilder sb = new StringBuilder();

        for (String s : mDesMap.values()) {

            sb.append("“" + s + "”");

        }
        String result = "已选择：" + sb.toString();
        return result;
    }


}
